package com.zhs.gccs.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.os.Build;
import android.os.Environment;
import android.widget.RelativeLayout;

import com.zhs.gccs.model.SmTask;
import com.zhs.gccs.view.WaterMaskView;

import java.io.File;
import java.io.IOException;

public class Utils {


    /**
     * 默认路径
     */
    public static String getMainPath(Context mContext) {
        String path = "";
        if (Build.VERSION.SDK_INT >= 29) {
            path = mContext.getExternalFilesDir("") + "/www/";
        }else {
            path = Environment.getExternalStoragePublicDirectory("").getPath()
                    + "/com.zhs.gccs/www/";
//            path = Environment.getExternalStorageDirectory() + "/com.zhs.gccs/www/";
        }
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        return path;
    }

    public static String getTaskRootFolder(Context mContext, SmTask task){
        String sdPath = Utils.getMainPath(mContext)+ task.getTaskid() ;
        return sdPath;
    }

    public static String getTaskFilesFolder(Context mContext, SmTask task){
        String sdPath = getTaskRootFolder(mContext,task) + "/" +  task.getTaskid() ;
        return sdPath;
    }



    public static String getTaskFeedbackFolder(Context mContext, SmTask task){
        String sdPath = getTaskRootFolder(mContext,task) + "/feedback/" + task.getTeskname()
                + "_" + task.getTaskid() + "/";
        return sdPath;
    }



    public static String getTaskGjFilePath(Context mContext, SmTask task){
        String sdPath = getTaskFeedbackFolder(mContext,task);
        //轨迹
        String gjFileName = "location_" + task.getTaskid() + ".json";
        return sdPath+gjFileName;
    }

    public static String getTaskGjFileContent(Context mContext, SmTask task){
        String file = getTaskGjFilePath(mContext,task);
        try {
           return FileUtils.readFileToString(new File(file),CharsetUtils.UTF8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将经纬度信息写入JPEG图片文件里
     *
     * @param picPath JPEG图片文件路径
     * @param dLat    纬度
     * @param dLon    经度
     */
    public static void writeLatLonIntoJpeg(String picPath,
                                           double dLat,
                                           double dLon,
                                           String sTime,String author,String comment
    ) {
        File file = new File(picPath);
        if (file.exists()) {
            try {
//                ExifInterface exif = new ExifInterface(new FileInputStream(file));
                ExifInterface exif = new ExifInterface(picPath);
                String tagLat = exif
                        .getAttribute(ExifInterface.TAG_GPS_LATITUDE);
                String tagLon = exif
                        .getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
                if (tagLat == null && tagLon == null) // 无经纬度信息
                {
                    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
                            LocationUtils.gpsInfoConvert(dLat));
                    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,
                            dLat > 0 ? "N" : "S"); // 区分南北半球
                    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
                            LocationUtils.gpsInfoConvert(dLon));
                    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,
                            dLon > 0 ? "E" : "W"); // 区分东经西经
                    exif.setAttribute(ExifInterface.TAG_DATETIME,sTime);
                    exif.setAttribute(ExifInterface.TAG_ARTIST,author);
//                    exif.setAttribute(ExifInterface.TAG_COPYRIGHT,author);
                    exif.setAttribute(ExifInterface.TAG_IMAGE_DESCRIPTION,comment);
                    exif.saveAttributes();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * @param position 左上为0，顺时针算起
     */
    public static Bitmap saveWaterMask(Context context,
                                        int position,
                                        Bitmap sourBitmap,
                                        String date,
                                        String lngAndLat,
                                        String address,
                                        String pid,
                                        String user) {
        WaterMaskView waterMaskView = new WaterMaskView(context);
        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        waterMaskView.setLayoutParams(params);
        waterMaskView.setCjrq(date);
        waterMaskView.setLng(lngAndLat);
        waterMaskView.setAddress(address);
        waterMaskView.setXmPid(pid);
        waterMaskView.setCjr(user);

        Bitmap waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);

        //根据原图处理要生成的水印的宽高
        float width = sourBitmap.getWidth();
        float height = sourBitmap.getHeight();
        float be = width / height;

//        if ((float) 16 / 9 >= be && be >= (float) 4 / 3) {
//            //在图片比例区间内16;9~4：3内，将生成的水印bitmap设置为原图宽高各自的1/5
//            waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) height / 5);
//        } else if (be > (float) 16 / 9) {
//            //生成4：3的水印
//            waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) width * 3 / 20);
//        } else if (be < (float) 4 / 3) {
//            //生成4：3的水印
//            waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) height * 4 / 15, (int) height / 5);
//        }
        Bitmap watermarkBitmap = null;
        switch (position) {
            case 0:
                watermarkBitmap = WaterMaskUtil.createWaterMaskLeftTop(context, sourBitmap, waterBitmap, 0, 0);
                break;
            case 1:
                watermarkBitmap = WaterMaskUtil.createWaterMaskRightTop(context, sourBitmap, waterBitmap, 0, 0);
                break;
            case 2:
                watermarkBitmap = WaterMaskUtil.createWaterMaskRightBottom(context, sourBitmap, waterBitmap, 0, 0);
                break;
            case 3:
                watermarkBitmap = WaterMaskUtil.createWaterMaskLeftBottom(context, sourBitmap, waterBitmap, 0, 0);
                break;
        }
        return watermarkBitmap;

    }

}
